/**
 * @file
 * @author 刘宽 (807874484@qq.com)
 * @brief header-template
 * @version 0.1
 * @date 2024-04-23
 */

#pragma once

#include <stdbool.h>
#include "main.h"
#include "usart.h"
#ifdef __cplusplus
extern "C" {
#endif

#define BSP_MAX_RECV_LEN (1024+32)

typedef enum {
    BSP_UART1    = (1 << 1),
    BSP_UART2    = (1 << 2),
    BSP_UART3    = (1 << 4),
} UART_ID;

typedef struct UART_STRUCT_S{
    //标识标识
    UART_ID id;
    //串口句柄
    UART_HandleTypeDef *handle;
    volatile bool is_init;         /* 串口组件初始化标志位 */
    volatile bool is_idle_int;     /* 是否发生空闲中断的标志位 */
    //自定义传送数据
    void *user_data;
    //------------私有数据请勿操作---------------
    //串口缓存区
    uint8_t  rx_buff[ BSP_MAX_RECV_LEN];
    uint16_t rx_buf_recv_len;
}UART_STRUCT;

//接收回调
typedef void (*UART_RECV_CB)(UART_STRUCT *uart);

//通过ID获取  UARTStruct
UART_STRUCT *bsp_uart_get_handle(UART_ID id);

uint8_t bsp_uart_init(UART_ID id);
//使能接收
uint8_t bsp_uart_enable_recv(UART_ID id);
//绑定用户数据
uint8_t  bsp_bind_user_data(UART_ID id, void *user_data);
//发送 指定端口数据
uint8_t  bsp_uart_send(UART_ID id,const uint8_t *data, uint16_t len, uint16_t timeout);
//收到一帧数据
bool  bsp_uart_is_frame_end(UART_ID id);
//获取一帧数据  return > 0 说明接到一帧数据
int bsp_get_frame(UART_ID id,void *data,int buf_size);

/**
 * @brief 关闭时能端口
 * @param id
 */
void bsp_uart_deint(UART_ID id);

//自定义的中断
void USER_UART_IRQHandler(UART_HandleTypeDef *huart);



#ifdef __cplusplus
} // extern "C"
#endif